package com.ijiuchuang.jc_localization.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.widget.AppCompatButton;
import android.support.v7.widget.AppCompatTextView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;

import com.ijiuchuang.jc_localization.R;
import com.ijiuchuang.jc_localization.base.BaseActivity;
import com.ijiuchuang.jc_localization.bean.entity.BleDevice;
import com.ijiuchuang.jc_localization.contract.tools.ToolsGuardsContract;
import com.ijiuchuang.jc_localization.presenter.tools.ToolsGuardsPresenter;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ToolsGuardsActivity extends BaseActivity implements ToolsGuardsContract.View {

    public static final String TOOLS_GUARDS_DEVICE = "ToolsGuardsActivity.bundle.device";

    @BindView(R.id.tools_guards_toolbar)
    Toolbar toolbar;
    @BindView(R.id.tools_guards_fab)
    FloatingActionButton fab;
    @BindView(R.id.tools_guards_coordinator)
    CoordinatorLayout coordinator;
    @BindView(R.id.tools_guards_text_state)
    TextView textState;
    @BindView(R.id.tools_guards_text_count)
    TextView textCount;
    @BindView(R.id.tools_guards_text_name)
    AppCompatTextView textName;
    @BindView(R.id.tools_guards_button_menu)
    AppCompatButton buttonMenu;
    @BindView(R.id.tools_guards_text_mac)
    AppCompatTextView textMac;
    @BindView(R.id.tools_guards_text_rssi_origin)
    AppCompatTextView textRssiOrigin;
    @BindView(R.id.tools_guards_text_heart_rate)
    AppCompatTextView textHeartRate;
    @BindView(R.id.tools_guards_text_power)
    AppCompatTextView textPower;

    private ToolsGuardsContract.Presenter presenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tools_guards);
        ButterKnife.bind(this);

        setSupportActionBar(toolbar);
        toolbar.setNavigationIcon(R.drawable.ic_arrow_back_white_24dp);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        presenter = new ToolsGuardsPresenter(this, this);
    }

    @Override
    protected void onStart() {
        super.onStart();
        presenter.start();
    }

    @Override
    protected void onStop() {
        super.onStop();
        presenter.stopScan();
    }

    @Override
    public BleDevice getDevice() {
        Bundle bundle = getIntent().getExtras();
        BleDevice bleDevice = (BleDevice) bundle.getSerializable(TOOLS_GUARDS_DEVICE);
        return bleDevice;
    }

    @Override
    public void openBle() {
        Snackbar.make(coordinator, R.string.snack_title_open_ble, Snackbar.LENGTH_INDEFINITE)
                .setAction(R.string.snack_action_ok, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        startActivity(new Intent(Settings.ACTION_BLUETOOTH_SETTINGS)); // 直接进入手机中的蓝牙设置界面
                    }
                })
                .setCallback(new Snackbar.Callback() {
                    @Override
                    public void onDismissed(Snackbar snackbar, int event) {
                        super.onDismissed(snackbar, event);

                        if (event == Snackbar.Callback.DISMISS_EVENT_SWIPE) {
                            finish();
                        }

                        if (!fab.isShown()) {
                            fab.show();
                        }
                    }

                    @Override
                    public void onShown(Snackbar snackbar) {
                        super.onShown(snackbar);

                        if (fab.isShown()) {
                            fab.hide();
                        }
                    }
                })
                .show();
    }

    @Override
    public void setFabIcon(int iconID) {
        fab.setImageResource(iconID);
    }

    @Override
    public void setDeviceName(String deviceName) {
        textName.setText(deviceName);
    }

    @Override
    public void setMac(String mac) {
        textMac.setText(mac);
    }

    @Override
    public void setRssiOrigin(String rssiOrigin) {
        textRssiOrigin.setText(rssiOrigin);
    }

    @Override
    public void setState(String state) {
        textState.setText(state);
    }

    @Override
    public void setHeartRate(String heartRate) {
        textHeartRate.setText(heartRate);
    }

    @Override
    public void setPower(String power) {
        textPower.setText(power);
    }

    @Override
    public void setCount(String count) {
        textCount.setText(count);
    }

    @Override
    public void setPresenter(ToolsGuardsContract.Presenter presenter) {
        this.presenter = presenter;
    }

    @OnClick({R.id.tools_guards_button_menu, R.id.tools_guards_fab})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tools_guards_button_menu:
                break;
            case R.id.tools_guards_fab:
                Snackbar.make(coordinator, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
                break;
        }
    }
}
